<?php
/**
 * @author wyl
* @date 2015-8-10 15:51:23
*/
Yii::import('system.web.widgets.CWidget');
class CTabsMenu extends CWidget{
	public $tabs=array();
	
	
	public function run(){
		$this->renderTabMenu();
		$this->renderTabContent();
		$this->buildScript();	
	}
	
	public function renderTabMenu(){
		echo CHtml::openTag('ul',array('class'=>'nav nav-tabs','role'=>'tablist'));
		foreach ($this->tabs as $tabsId=>$tab){
			$class='';
			if(array_key_exists('class', $tab)){
				$class=$tab['class'];
			}
			echo CHtml::openTag('li',array('role'=>'presentation','class'=>$class));
			echo CHtml::link($tab['title'],'#'.$tabsId,array(
					'role'			=>'tab',
					'data-toggle'	=>'tab',
					'aria-controls' =>$tabsId
			));
			echo CHtml::closeTag('li');
		}
		echo CHtml::closeTag('ul');	
	}
	
	public function renderTabContent(){
		echo CHtml::openTag('div',array('class'=>'tab-content'));
		foreach ($this->tabs as $tabsId=>$tab){
			$class='tab-pane';
			if(array_key_exists('class', $tab) &&  strstr($tab['class'],'active')){
				$class='tab-pane active';
			}
			echo CHtml::openTag('div',array('role'=>'tabpanel','class'=>$class,'id'=>$tabsId));
			echo CHtml::openTag('iframe',array(
											'src'			=>$tab['url'],
											'frameborder'	=>0,
											'scrolling'		=>'no',
											'id'			=>$tabsId.'-iframe',
											'width'			=>'100%',
											'name'			=>$tabsId
							));	
			Yii::app()->clientScript->registerScript($tabsId.'-iframe-script',"
					$(document).ready(function(){
						$('#".$tabsId."-iframe').on('load',function(){
						 	//var mainheight = $(this).contents().find('body').height()+30;
							//var mainheight = $(document).frames($tabsId.'-iframe-script').document.height()+30;
							//$(this).height(mainheight);
						}); 
					}) 
			");  
			echo CHtml::closeTag('iframe');
			echo CHtml::closeTag('div');
		}
		echo CHtml::closeTag('div');
	}
	
	public function buildScript(){
		Yii::app()->clientScript->registerCss(time(),"
				.nav.nav-tabs>li>a{
					color:#1E2225;
				}
				.nav.nav-tabs{
					border-bottom:solid 1px #1E2225;
				}
				.nav.nav-tabs>li.active>a{
					color:#fff;
					background-color:#1E2225;
				}
		");
	}
}		
